import 'package:flutter/cupertino.dart';
import 'package:flutter/widgets.dart';

import '../../public.dart';
import 'mixins/widget_base.dart';

/// 亮度调节（日夜模式切换）
class ReaderLight extends StatefulWidget {
  ReaderLight({Key key}) : super(key: key);

  @override
  ReaderLightState createState() => new ReaderLightState();
}

class ReaderLightState extends State<ReaderLight>
    with ReaderWidgetBase, TickerProviderStateMixin {
  ThemeProvider theme;
  AnimationController controller;
  bool isNight = false;

  @override
  void initState() {
    super.initState();
    controller =
        new AnimationController(vsync: this, duration: Duration(seconds: 1));

    WidgetsBinding.instance.addPostFrameCallback((_) {
      theme = Provider.of<ThemeProvider>(context, listen: false);
    });
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
        child: ImagesAnimation(
            controller: controller,
            w: 46,
            h: 58,
            entry: ImagesAnimationEntry(
                1, 12, AppConfig.ASSETS_PATH + "lights/%s.png")),
        onTap: () {
          !isNight ? controller.forward() : controller.reverse();
          isNight = !isNight;
          theme.setLight(isNight ? 125 : 0);
        });
  }
}
